package com.yoomiito.app.ui.list

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.scwang.smartrefresh.header.MaterialHeader
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.adapter.goods.list.NewBaoPinAdapter
import com.yoomiito.app.adapter.goods.list.NewGiftAdapter
import com.yoomiito.app.adapter.goods.list.NewTaoBaoAdapter
import com.yoomiito.app.adapter.goods.list.NewYouPinAdapter
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.BaseList
import com.yoomiito.app.model.gift.GiftGoods
import com.yoomiito.app.model.home.FeaturedGoodsInfo
import com.yoomiito.app.utils.AllCode
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.RouterUtils
import com.yoomiito.app.widget.SpacingDecoration
import com.yoomiito.app.widget.state.ContentType
import com.yoomiito.app.widget.state.EmptyType
import com.yoomiito.app.widget.state.StatusManager
import kotlinx.android.synthetic.main.title_bar.*
import kotlinx.android.synthetic.main.act_title_framelayout.*
import kotlinx.android.synthetic.main.refresh_rcy_layout.*

/**
 * 不带顶部图的列表
 */
@Deprecated("不再使用")
class GoodsListActivity : BaseActivity<GoodsListPresenter>() {

    companion object {
        fun start(context: Context, type: String, id: String, title: String) {
            val intent = Intent(context, GoodsListActivity::class.java)
            intent.putExtra(Constant.ID, id)
            intent.putExtra(Constant.TYPE, type)
            intent.putExtra(Constant.KEY_TITLE, title)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)
        }
    }

    lateinit var id: String
    private var mCurrentPage = 1
    lateinit var mAdapter: BaseQuickAdapter<GiftGoods, BaseViewHolder>

    override fun getLayoutId(): Int = R.layout.act_title_framelayout

    override fun newP(): GoodsListPresenter = GoodsListPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        id = intent.getStringExtra(Constant.ID)

        initView(intent.getStringExtra(Constant.KEY_TITLE))
        initRcy(intent.getStringExtra(Constant.TYPE))
        initRefresh()

    }

    private fun initView(title: String) {
        iv_back_left.setOnClickListener { finish() }
        tv_center.text = title

        mStatusManager = StatusManager.Builder(this)
                .addType(ContentType(R.layout.refresh_rcy_layout))
                .addType(EmptyType().setEmptyContent("未找到相关商品"))
                .build()
        act_title_fm.addView(mStatusManager.view())
    }

    private fun initRefresh() {
        refreshLayout.setBackgroundResource(R.color.color_F4F4F4)
        refreshLayout.setRefreshHeader(MaterialHeader(this))
        refreshLayout.setRefreshFooter(ClassicsFooter(this))
        refreshLayout.setOnRefreshListener {
            mCurrentPage = 1
            p.getList(id, mCurrentPage)
        }
        refreshLayout.setOnLoadMoreListener {
            mCurrentPage++
            p.getList(id, mCurrentPage)
        }
        refreshLayout.autoRefresh()
    }

    private fun initRcy(type: String) {
        val lp = rcy.layoutParams as SmartRefreshLayout.LayoutParams
        val margin = DensityUtil.dp2px(12f)
        lp.topMargin = margin
        lp.leftMargin = margin
        lp.rightMargin = margin
        rcy.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        val sd = SpacingDecoration(0, DensityUtil.dp2px(8f), false)
        rcy.addItemDecoration(sd)
        //'1淘宝商品2礼包商品3爆品商品4优品商品5区代商品',
        when (type) {
            "1" -> {
                rcy.removeItemDecoration(sd)
                rcy.layoutManager = androidx.recyclerview.widget.GridLayoutManager(this, 2)
                val space = DensityUtil.dp2px(5f)
                rcy.addItemDecoration(SpacingDecoration(space, space, false))
                mAdapter = NewTaoBaoAdapter(null)
            }
            "2" -> mAdapter = NewGiftAdapter(null)
            "3" -> mAdapter = NewBaoPinAdapter(null)
            "4" -> mAdapter = NewYouPinAdapter(null)
            "5" -> mAdapter = NewYouPinAdapter(null)
        }
        rcy.adapter = mAdapter
        mAdapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
            var info = mAdapter.data[position]
            RouterUtils.toGoodsDetail(this, info.id.toString(), AllCode.getGoodsType(if(info.type==1) 1 else 2,info.product_type,-1))
        }

    }

    fun setData(t: BaseList<GiftGoods>) {
        mCurrentPage = t.current_page
        if (mCurrentPage == 1) {
            if (t.data.isNullOrEmpty()) mStatusManager.showEmpty() else mAdapter.setNewData(t.data)
            refreshLayout.finishRefresh()
        } else {
            mAdapter.addData(t.data)
            refreshLayout.finishLoadMore()
        }
        if (mCurrentPage == t.last_page) {
            refreshLayout.finishLoadMoreWithNoMoreData()
        }
    }

}